import json

from aliyunsdkdysmsapi.request.v20170525 import SendSmsRequest
from aliyunsdkcore.client import AcsClient
from aliyunsdkcore.profile import region_provider

# 注意：不要更改
REGION = 'cn-hangzhou'
PRODUCT_NAME = 'Dysmsapi'
DOMAIN = 'dysmsapi.aliyuncs.com'
ACCESS_KEY_ID = 'LTAI5tQB1BKfvf1JPArwxjpw'
ACCESS_KEY_SECRET = 'GqZWZEH0nVlQVTvXWPwb9q1X7QPnwp'

acs_client = AcsClient(ACCESS_KEY_ID, ACCESS_KEY_SECRET, REGION)
region_provider.add_endpoint(PRODUCT_NAME, REGION, DOMAIN)

'''
b'{"RequestId":"FC3E2872-4AE5-5AFB-9CDB-3E7343C3ACDA","Message":"OK","BizId":"296615441797963430^0","Code":"OK"}'
'''


def send_sms(business_id, phone_numbers, sign_name, template_code, template_param=None):
    smsRequest = SendSmsRequest.SendSmsRequest()
    # 申请的短信模板编码,必填
    smsRequest.set_TemplateCode(template_code)
    # 短信模板变量参数
    if template_param is not None:
        smsRequest.set_TemplateParam(template_param)
    # 设置业务请求流水号，必填。
    smsRequest.set_OutId(business_id)
    # 短信签名
    smsRequest.set_SignName(sign_name)
    # 数据提交方式
    # smsRequest.set_method(MT.POST)
    # 数据提交格式
    # smsRequest.set_accept_format(FT.JSON)
    # 短信发送的号码列表，必填。
    smsRequest.set_PhoneNumbers(phone_numbers)
    # 调用短信发送接口，返回json
    smsResponse = acs_client.do_action_with_exception(smsRequest)
    resp_obj = {}
    try:
        resp_obj = json.loads(smsResponse)
    except:
        pass
    return resp_obj
